﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System
{

	/// <summary>
	/// Métodos helpers para trabalhar com strings
	/// </summary>
	public static class StringExtensions
	{

		/// <summary>
		/// Retorna a string invertida
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string Reverse( this string str )
		{
			if ( str == null )
				return null;
			else
				return new String( Enumerable.Reverse( str ).ToArray() );
		}

		/// <summary>
		/// Junta o array de caracteres
		/// </summary>
		/// <param name="chain"></param>
		/// <param name="prefix"></param>
		/// <param name="separator"></param>
		/// <param name="sufix"></param>
		/// <returns></returns>
		public static string Join( this IEnumerable<char> chain, string prefix, string separator, string sufix )
		{
			if ( chain == null )
			{
				return String.Concat( prefix, sufix );
			}
			else
			{

				//int prefixLen = (prefix== null ? 0: prefix.Length);
				//int sufixLen = (sufix == null ? 0: sufix.Length );
				int len = ( separator.Length * ( chain.Count() ) );

				StringBuilder result = new StringBuilder( len );

				result.Append( prefix );

				var iterator = chain.GetEnumerator();
				if ( iterator.MoveNext() )
				{
					result.Append( iterator.Current );

					while ( iterator.MoveNext() )
					{
						result.Append( separator ).Append( iterator.Current );
					}
				}

				result.Append( sufix );
				return result.ToString();
			}
		}
	}
}
